﻿namespace TestTool.GUI.Utils
{
    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using System;
    using System.Runtime.CompilerServices;

    public class CustomPageEventHelper : PdfPageEventHelper
    {
        public override void OnEndPage(PdfWriter writer, Document document)
        {
            base.OnEndPage(writer, document);
            PdfContentByte directContent = writer.DirectContent;
            Rectangle pageSize = document.PageSize;
            PdfPTable table = new PdfPTable(3) {
                TotalWidth = (pageSize.Width - document.LeftMargin) - document.RightMargin
            };
            PdfPCell cell = new PdfPCell(new Phrase(this.FootherFirst, this.FooterFont)) {
                HorizontalAlignment = 1,
                Border = 0
            };
            table.AddCell(cell);
            PdfPCell cell2 = new PdfPCell(new Phrase(this.FootherSecond, this.FooterFont)) {
                HorizontalAlignment = 1,
                Border = 0
            };
            table.AddCell(cell2);
            PdfPCell cell3 = new PdfPCell(new Phrase(this.FootherThird, this.FooterFont)) {
                HorizontalAlignment = 1,
                Border = 0
            };
            table.AddCell(cell3);
            directContent.SetRGBColorFill(0, 0, 0);
            table.WriteSelectedRows(0, -1, pageSize.GetLeft(document.LeftMargin), pageSize.GetBottom(40f), directContent);
        }

        public override void OnStartPage(PdfWriter writer, Document document)
        {
            base.OnStartPage(writer, document);
            PdfContentByte directContent = writer.DirectContent;
            Rectangle pageSize = document.PageSize;
            PdfPTable table = new PdfPTable(2) {
                TotalWidth = (pageSize.Width - document.LeftMargin) - document.RightMargin
            };
            table.SetWidthPercentage(new float[] { 20f, 80f }, pageSize);
            PdfPCell cell = new PdfPCell(this.HeaderLogo, true) {
                HorizontalAlignment = 2,
                Border = 0
            };
            table.AddCell(cell);
            PdfPCell cell2 = new PdfPCell(new Phrase(5f, this.HeaderText, this.HeaderFont)) {
                HorizontalAlignment = 0,
                VerticalAlignment = 6,
                Border = 0
            };
            table.AddCell(cell2);
            directContent.SetRGBColorFill(0, 0, 0);
            table.WriteSelectedRows(0, -1, pageSize.GetLeft(document.LeftMargin), pageSize.GetTop(40f), directContent);
        }

        public iTextSharp.text.Font FooterFont { get; set; }

        public string FootherFirst { get; set; }

        public string FootherSecond { get; set; }

        public string FootherThird { get; set; }

        public iTextSharp.text.Font HeaderFont { get; set; }

        public Image HeaderLogo { get; set; }

        public string HeaderText { get; set; }
    }
}

